﻿###########常用模块############3333
########### datetime
from datetime import datetime
now = datetime.now();
print(now)
#获取指定日期和时间
dt = datetime(2015,4,19,12,20)
print(dt)

#######collections集合模块
from collections import namedtuple
Point = namedtuple("Point",["x","y"])
p = Point(1,2)
print(p.x);
print(p.y);

##########deque 这个的插入和删除比线性的list快 应该是链表吧
from collections import deque
q = deque(['a','b','c'])
print(q);

#########defaultdict 有默认函数的字典
from collections import defaultdict
dd = defaultdict(lambda:"N/A")
dd["key1"] = "abc"
print(dd["key1"])

##########Counter简单的计数器
from collections import Counter
c = Counter();
for ch in "dasfdasfdsasd":
	c[ch] = c[ch] + 1
print(c)	

#########hashlib 密码处理
import hashlib
md5 = hashlib.md5();
md5.update("asdfgh98765*".encode("utf-8"));
print(md5.hexdigest());

##########itertools 迭代器 无限迭代器
# import itertools
# natu = itertools.count(1)
# for n in natu:
	# print(n);
	
#########contextlib 没太懂 好像是能用with
class Query(object): 
	def __init__(self):
		print("init")
	
	def __enter__(self):
		print("enter")

	def __exit__(self,arg1,arg2,arg3):
		print("exit")
	
	def query(self):
		print("query")

q = Query();
q.query();
with Query() as q:
	pass
	# q.query();
	
#########xml解析 sax解析
# from xml.parsers.expat import ParserCreate

# class DefaultSaxHandler(object):
    # def start_element(self, name, attrs):
        # print('sax:start_element: %s, attrs: %s' % (name, str(attrs)))

    # def end_element(self, name):
        # print('sax:end_element: %s' % name)

    # def char_data(self, text):
        # print('sax:char_data: %s' % text)
# xml = r'''<?xml version="1.0"?>
# <ol>
    # <li><a href="/python">Python</a></li>
    # <li><a href="/ruby">Ruby</a></li>
# </ol>
# '''
# handler = DefaultSaxHandler();
# parser = ParserCreate();
# parser.StartElementHandler = handler.start_element
# parser.EndElementHandler = handler.end_element
# parser.CharacterDataHandler = handler.char_data
# parser.Parse(xml)

#########urllib解析
###get请求
from urllib import request
with request.urlopen('https://api.douban.com/v2/book/2129650') as f:
	data = f.read();
	print("status:",f.status,f.reason);
	for k,v in f.getheaders():
		print(k,v)
	print("data:",data.decode("utf-8"))
	
#请求手机版的豆瓣网站
req = request.Request('http://www.douban.com/')
req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
with request.urlopen(req) as f:
    print('Status:', f.status, f.reason)
    for k, v in f.getheaders():
        print('%s: %s' % (k, v))
    print('Data:', f.read().decode('utf-8'))
	
###post发送一个请求
